var base = require('/base_info.js');
var utils = require('/utils/index.js');
// 富文本解析
var wxParse = require('/pages/wxParse-master/wxParse.js');
Page({
  data: {
    cardInfo: {},
    imgUrl: base.imgUrl
  },
  getData (id) {
    let that = this;
    utils.getData('SingleEquityCardArticle', {
      user_id: my.getStorageSync({
        key: 'uid', // 缓存数据的key
      }).data,
      id: id
    }, (res) => {
      console.log(res.data.data);
      my.hideLoading({
        page: that  // 防止执行时已经切换到其它页面，page指向不准确
      });
      that.setData({
        cardInfo: res.data.data
      });
      that.getCardUrl();
      wxParse.wxParse('introduce', 'html', res.data.data.introduce, that, 5);
      wxParse.wxParse('privilege', 'html', res.data.data.privilege, that, 5);
    }, (err) => {
      my.hideLoading({
        page: that  // 防止执行时已经切换到其它页面，page指向不准确
      });
      console.log(err);
    });
  },
  // 跳转到协议内容
  agreementDetail () {
    my.navigateTo({
      url: './../agreement/agreement?id=' + this.data.cardInfo.id
    }); 
  },
  // 去使用
  goUse () {
    my.navigateBack({
      delta: 100
    });
  },
  // 加入卡包
  addCardClick() {
    let that = this;
    if (!that.data.apply_card_url || that.data.apply_card_url == "") {
      my.alert({
        content: '网络异常，请稍后再试'
      });
      return false;
    }
    my.showLoading();
    new Promise((resolve, reject) => {
      my.addCardAuth({
        url: that.data.apply_card_url,
        success: (res) => {
          if (res.success) {
            let data = res.result;
            data.external_card_no = that.data.cardInfo.card_no;
            resolve(data);
          } else {
            reject(1);
          };
        },
        fail: () => {
          reject(1);
        }
      })
    }).then((data) => {
      // 加入卡包
      utils.getData('CardOpen', data, (res) => {
        if (res.data.message == 'success') {
          my.hideLoading();
          my.alert({
            content: '已成功加入卡包'
          });
          that.setData({
            addCardSign: !that.data.addCardSign
          });
          return false;
        }
        my.alert({
          content: '加入卡包失败' + JSON.stringify(res)
        });
      }, (err) => {
        my.hideLoading();
        my.alert({
          content: '加入卡包失败'
        });
      });
    }, (err) => {
      my.hideLoading();
      my.alert({
        content: '加入卡包失败'
      });
    });
  },
  // 获取卡片url
  // 获取加入卡包的链接地址
  getCardUrl () {
    let that = this;
    utils.getData('Activateurl', {
      template_id: that.data.cardInfo.template_id
    }, (res) => {
      that.setData({
        apply_card_url: res.data.data.apply_card_url
      });
    }, (err) => {
      console.log(err);
    });
  },
  onLoad(query) {
    console.log(query);
    my.showLoading();
    this.getData(query.id);
  },
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: my.getStorageSync({
        key: 'hotel_name', // 缓存数据的key
      }).data,
      desc: my.getStorageSync({
        key: 'my_app_description', // 缓存数据的key
      }).data,
      path: 'pages/index/index?share=true',
      success: function (e) {
        console.log(e);
      }
    };
  },
});
